package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.qiantu.api.entity.MemberBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;

import java.util.LinkedHashMap;
import java.util.List;

public final class PushReceiverAdapter extends AppAdapter<MemberBean> {

    private LinkedHashMap<String, MemberBean> selected = new LinkedHashMap();
    public PushReceiverAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvReceiverName;
        private final TextView tvReceiverNickName;
        private final TextView checkView;

        private ViewHolder() {
            super(R.layout.item_push_receiver);
            tvReceiverName = findViewById(R.id.tv_receiver_name);
            tvReceiverNickName = findViewById(R.id.tv_receiver_nickname);
            checkView = findViewById(R.id.check_view);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            MemberBean data = (MemberBean) itemView.getTag();
            checkView.setSelected(!checkView.isSelected());
            if(checkView.isSelected()){
                selected.put(data.getUserSerialNo(), data);
            }else{
                selected.remove(data.getUserSerialNo());
            }
            super.onClick(view);
        }

        @Override
        public void onBindView(int position) {
            MemberBean member = getItem(position);
            if(selected.get(member.getUserSerialNo()) != null){
                checkView.setSelected(true);
            }else{
                checkView.setSelected(false);
            }
            tvReceiverName.setText(member.getUserNickName());
            tvReceiverNickName.setText(member.getMemberNickName());
            itemView.setTag(member);
        }
    }

    public LinkedHashMap getSelected() {
        return selected;
    }

//    public void checked(List<MemberBean> checked){
//        selected.clear();
//        if(checked != null && checked.size() > 0){
//            for(MemberBean member : checked){
//                selected.put(member.getUserSerialNo(), member);
//            }
//            notifyDataSetChanged();
//        }
//    }

    public void checked(String checked){
        selected.clear();
        for(MemberBean member : getData()){
            if(member.getUserSerialNo().equals(checked)){
                selected.put(checked, member);
                break;
            }
        }
        notifyDataSetChanged();
    }

    public void checked(List<String> checked){
        selected.clear();
        if(checked != null && checked.size() > 0){
            for(String value : checked){
                for(MemberBean member : getData()){
                    if(member.getUserSerialNo().equals(value)){
                        selected.put(value, member);
                        break;
                    }
                }
            }
        }
        notifyDataSetChanged();
    }
}